package com.cn.kabalifemerchants.ui.login;


import com.cn.kabalifemerchants.api.Api;
import com.cn.kabalifemerchants.base.BasePresenter;
import com.cn.kabalifemerchants.callback.CacheCallBack;
import com.cn.kabalifemerchants.config.Constants;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 晓林 on 2020/9/23.
 * function:
 */
public class ForgetPwdPresenter extends BasePresenter<ForgetPwdView> {
    /**
     * 验证码
     * @param phone
     * @param type
     */
    public void onPhoneCodeData(String phone, int type) {
        OkGo.<MsgBean>post(Api.API_LOGIN_MSG)
                .params("phone", phone)
                .params("type", type)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("手机验证码：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onPhoneCodeSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 修改密码
     * @param phone
     * @param pwd
     * @param code
     */
    public void onPwdUpData(String phone, String pwd, String code) {
        OkGo.<MsgBean>post(Api.API_PWD_UP)
                .params("phone", phone)
                .params("pwd", pwd)
                .params("code", code)
                .execute(new CacheCallBack<MsgBean>(getView()._getContext(), true) {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改面：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onPwdUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

}
